<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>13.restful-api request response | winnnntttter</title>
    <meta name="description" content="">
    <meta name="generator" content="VuePress 1.8.0">
    <link rel="icon" href="/favicon.ico">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.11.1/dist/katex.min.css">
    <script>
      var _hmt = _hmt || [];
      (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?7ae3e8df51439cebe7b2a43bccd0b253";
        var s = document.getElementsByTagName("script")[0]; 
        s.parentNode.insertBefore(hm, s);
      })();
      </script>
    <meta name="description" content="winnnntttter的个人站">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    <link rel="preload" href="/assets/css/0.styles.a3a399df.css" as="style"><link rel="preload" href="/assets/js/app.419c42a0.js" as="script"><link rel="preload" href="/assets/js/3.2e7f4496.js" as="script"><link rel="preload" href="/assets/js/58.0b637d75.js" as="script"><link rel="prefetch" href="/assets/js/10.bd166216.js"><link rel="prefetch" href="/assets/js/100.ef6c4e60.js"><link rel="prefetch" href="/assets/js/101.d01225fc.js"><link rel="prefetch" href="/assets/js/102.b85e1bb3.js"><link rel="prefetch" href="/assets/js/103.efa64f76.js"><link rel="prefetch" href="/assets/js/104.e62be1cd.js"><link rel="prefetch" href="/assets/js/105.4db56e9c.js"><link rel="prefetch" href="/assets/js/106.f6ccef65.js"><link rel="prefetch" href="/assets/js/11.705ff9c2.js"><link rel="prefetch" href="/assets/js/12.7f08fe83.js"><link rel="prefetch" href="/assets/js/13.c9276fff.js"><link rel="prefetch" href="/assets/js/14.03705b05.js"><link rel="prefetch" href="/assets/js/15.a39a18a1.js"><link rel="prefetch" href="/assets/js/16.ecd9a98c.js"><link rel="prefetch" href="/assets/js/17.cfa099fe.js"><link rel="prefetch" href="/assets/js/18.be92763c.js"><link rel="prefetch" href="/assets/js/19.8a4801fa.js"><link rel="prefetch" href="/assets/js/20.bfc1f073.js"><link rel="prefetch" href="/assets/js/21.08ea81e0.js"><link rel="prefetch" href="/assets/js/22.635463a2.js"><link rel="prefetch" href="/assets/js/23.fcf1e19e.js"><link rel="prefetch" href="/assets/js/24.e4fe2627.js"><link rel="prefetch" href="/assets/js/25.c64ebd97.js"><link rel="prefetch" href="/assets/js/26.b83aa917.js"><link rel="prefetch" href="/assets/js/27.9c0001f7.js"><link rel="prefetch" href="/assets/js/28.467acfc5.js"><link rel="prefetch" href="/assets/js/29.b88eaf80.js"><link rel="prefetch" href="/assets/js/30.79fca896.js"><link rel="prefetch" href="/assets/js/31.f0b09253.js"><link rel="prefetch" href="/assets/js/32.680d05e2.js"><link rel="prefetch" href="/assets/js/33.06db5f96.js"><link rel="prefetch" href="/assets/js/34.9b7ffb83.js"><link rel="prefetch" href="/assets/js/35.b302d83e.js"><link rel="prefetch" href="/assets/js/36.8ce84d21.js"><link rel="prefetch" href="/assets/js/37.8f1286d0.js"><link rel="prefetch" href="/assets/js/38.0eba62cc.js"><link rel="prefetch" href="/assets/js/39.9eafaf04.js"><link rel="prefetch" href="/assets/js/4.10fce449.js"><link rel="prefetch" href="/assets/js/40.94e167a7.js"><link rel="prefetch" href="/assets/js/41.9b634e18.js"><link rel="prefetch" href="/assets/js/42.a3ad7d5f.js"><link rel="prefetch" href="/assets/js/43.9ad38a47.js"><link rel="prefetch" href="/assets/js/44.1d5a8a2c.js"><link rel="prefetch" href="/assets/js/45.45af274c.js"><link rel="prefetch" href="/assets/js/46.45a0da7e.js"><link rel="prefetch" href="/assets/js/47.e31b4c4f.js"><link rel="prefetch" href="/assets/js/48.2b1a4ffb.js"><link rel="prefetch" href="/assets/js/49.e4a82034.js"><link rel="prefetch" href="/assets/js/5.edc6b3fb.js"><link rel="prefetch" href="/assets/js/50.8b0a75d7.js"><link rel="prefetch" href="/assets/js/51.fb58d1f2.js"><link rel="prefetch" href="/assets/js/52.aba39728.js"><link rel="prefetch" href="/assets/js/53.d683a969.js"><link rel="prefetch" href="/assets/js/54.c91b9f4c.js"><link rel="prefetch" href="/assets/js/55.76d17213.js"><link rel="prefetch" href="/assets/js/56.5935f2ad.js"><link rel="prefetch" href="/assets/js/57.65d32203.js"><link rel="prefetch" href="/assets/js/59.4f569adb.js"><link rel="prefetch" href="/assets/js/6.fe59ecb4.js"><link rel="prefetch" href="/assets/js/60.3f56e6a2.js"><link rel="prefetch" href="/assets/js/61.a6beb596.js"><link rel="prefetch" href="/assets/js/62.db0af84e.js"><link rel="prefetch" href="/assets/js/63.1d585303.js"><link rel="prefetch" href="/assets/js/64.3861310e.js"><link rel="prefetch" href="/assets/js/65.9b1ef5df.js"><link rel="prefetch" href="/assets/js/66.dd5713f3.js"><link rel="prefetch" href="/assets/js/67.fa823bb5.js"><link rel="prefetch" href="/assets/js/68.ae4e8b4f.js"><link rel="prefetch" href="/assets/js/69.b36498fb.js"><link rel="prefetch" href="/assets/js/7.04e3d394.js"><link rel="prefetch" href="/assets/js/70.8f75877a.js"><link rel="prefetch" href="/assets/js/71.8b54fe2e.js"><link rel="prefetch" href="/assets/js/72.7892e629.js"><link rel="prefetch" href="/assets/js/73.41e6a3ad.js"><link rel="prefetch" href="/assets/js/74.a8d720c9.js"><link rel="prefetch" href="/assets/js/75.677c8235.js"><link rel="prefetch" href="/assets/js/76.9c7df0e7.js"><link rel="prefetch" href="/assets/js/77.1e4ef451.js"><link rel="prefetch" href="/assets/js/78.503ea59d.js"><link rel="prefetch" href="/assets/js/79.e473582d.js"><link rel="prefetch" href="/assets/js/8.35d757ca.js"><link rel="prefetch" href="/assets/js/80.71d04c61.js"><link rel="prefetch" href="/assets/js/81.36d69e64.js"><link rel="prefetch" href="/assets/js/82.9491f2b1.js"><link rel="prefetch" href="/assets/js/83.d9808b8b.js"><link rel="prefetch" href="/assets/js/84.e0231735.js"><link rel="prefetch" href="/assets/js/85.ddc29777.js"><link rel="prefetch" href="/assets/js/86.05cd7dc6.js"><link rel="prefetch" href="/assets/js/87.1e74cca5.js"><link rel="prefetch" href="/assets/js/88.957d6dbb.js"><link rel="prefetch" href="/assets/js/89.b216c556.js"><link rel="prefetch" href="/assets/js/9.07b2db50.js"><link rel="prefetch" href="/assets/js/90.0c71a3f2.js"><link rel="prefetch" href="/assets/js/91.038eb70a.js"><link rel="prefetch" href="/assets/js/92.f05b7ced.js"><link rel="prefetch" href="/assets/js/93.e1cd3406.js"><link rel="prefetch" href="/assets/js/94.fe506104.js"><link rel="prefetch" href="/assets/js/95.440323e0.js"><link rel="prefetch" href="/assets/js/96.9ee51a0b.js"><link rel="prefetch" href="/assets/js/97.826f8538.js"><link rel="prefetch" href="/assets/js/98.15cf8919.js"><link rel="prefetch" href="/assets/js/99.489906fc.js"><link rel="prefetch" href="/assets/js/vendors~docsearch.f879b1e1.js">
    <link rel="stylesheet" href="/assets/css/0.styles.a3a399df.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="ant-row"><div class="sidebar-button"><i aria-label="icon: bars" class="anticon anticon-bars"><svg viewBox="0 0 1024 1024" focusable="false" data-icon="bars" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M912 192H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 284H328c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h584c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM104 228a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0zm0 284a56 56 0 1 0 112 0 56 56 0 1 0-112 0z"></path></svg></i> <span></span></div> <div class="ant-col ant-col-xs-24 ant-col-sm-24 ant-col-md-6 ant-col-lg-5 ant-col-xl-5 ant-col-xxl-4"><a href="/" class="router-link-active home-link"><img src="/logo.jpg" alt="winnnntttter" class="logo"> <span class="site-name">winnnntttter</span></a> <div class="search-box mobile-search"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div></div> <div class="ant-col ant-col-xs-0 ant-col-sm-0 ant-col-md-18 ant-col-lg-19 ant-col-xl-19 ant-col-xxl-20"><form id="search-form" role="search" class="algolia-search-wrapper search-box"><input id="algolia-search-input" class="search-query"></form> <nav class="nav-links can-hide"><ul role="menu" id="nav" class="ant-menu ant-menu-horizontal ant-menu-root ant-menu-light"><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/" class="router-link-active">
          首页
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-submenu-selected"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🍃前端
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="/Flutter/">
          🌿Flutter
        </a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🌱其他
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-item"><a href="https://jsbin.com/" target="_blank">
          🏃‍♂️运行
          <i aria-label="icon: link" class="anticon anticon-link"><svg viewBox="64 64 896 896" focusable="false" data-icon="link" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M574 665.4a8.03 8.03 0 0 0-11.3 0L446.5 781.6c-53.8 53.8-144.6 59.5-204 0-59.5-59.5-53.8-150.2 0-204l116.2-116.2c3.1-3.1 3.1-8.2 0-11.3l-39.8-39.8a8.03 8.03 0 0 0-11.3 0L191.4 526.5c-84.6 84.6-84.6 221.5 0 306s221.5 84.6 306 0l116.2-116.2c3.1-3.1 3.1-8.2 0-11.3L574 665.4zm258.6-474c-84.6-84.6-221.5-84.6-306 0L410.3 307.6a8.03 8.03 0 0 0 0 11.3l39.7 39.7c3.1 3.1 8.2 3.1 11.3 0l116.2-116.2c53.8-53.8 144.6-59.5 204 0 59.5 59.5 53.8 150.2 0 204L665.3 562.6a8.03 8.03 0 0 0 0 11.3l39.8 39.8c3.1 3.1 8.2 3.1 11.3 0l116.2-116.2c84.5-84.6 84.5-221.5 0-306.1zM610.1 372.3a8.03 8.03 0 0 0-11.3 0L372.3 598.7a8.03 8.03 0 0 0 0 11.3l39.6 39.6c3.1 3.1 8.2 3.1 11.3 0l226.4-226.4c3.1-3.1 3.1-8.2 0-11.3l-39.5-39.6z"></path></svg></i></a></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="display:none;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>
          🌈联系
        </span><i class="ant-menu-submenu-arrow"></i></div></li><li role="menuitem" class="ant-menu-submenu ant-menu-submenu-horizontal ant-menu-overflowed-submenu" style="visibility:hidden;position:absolute;"><div aria-haspopup="true" class="ant-menu-submenu-title"><span>···</span><i class="ant-menu-submenu-arrow"></i></div></li></ul> <a href="https://github.com/winnnntttter/winnnntttter.github.io" target="_blank" rel="noopener noreferrer" class="repo-link"><i aria-label="icon: github" class="anticon anticon-github"><svg viewBox="64 64 896 896" focusable="false" data-icon="github" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M511.6 76.3C264.3 76.2 64 276.4 64 523.5 64 718.9 189.3 885 363.8 946c23.5 5.9 19.9-10.8 19.9-22.2v-77.5c-135.7 15.9-141.2-73.9-150.3-88.9C215 726 171.5 718 184.5 703c30.9-15.9 62.4 4 98.9 57.9 26.4 39.1 77.9 32.5 104 26 5.7-23.5 17.9-44.5 34.7-60.8-140.6-25.2-199.2-111-199.2-213 0-49.5 16.3-95 48.3-131.7-20.4-60.5 1.9-112.3 4.9-120 58.1-5.2 118.5 41.6 123.2 45.3 33-8.9 70.7-13.6 112.9-13.6 42.4 0 80.2 4.9 113.5 13.9 11.3-8.6 67.3-48.8 121.3-43.9 2.9 7.7 24.7 58.3 5.5 118 32.4 36.8 48.9 82.7 48.9 132.3 0 102.2-59 188.1-200 212.9a127.5 127.5 0 0 1 38.1 91v112.5c.8 9 0 17.9 15 17.9 177.1-59.7 304.6-227 304.6-424.1 0-247.2-200.4-447.3-447.5-447.3z"></path></svg></i></a></nav></div></div> <!----></header> <aside class="sidebar"><!----> <ul class="sidebar-links"><li><a href="/front-end/node/" aria-current="page" title="1.基本定义" class="sidebar-link">1.基本定义</a></li><li><a href="/front-end/node/1.基本定义.html" title="1.基本定义" class="sidebar-link">1.基本定义</a></li><li><a href="/front-end/node/10. express1.html" title="10. express1" class="sidebar-link">10. express1</a></li><li><a href="/front-end/node/11.middleware.html" title="11.middleware" class="sidebar-link">11.middleware</a></li><li><a href="/front-end/node/12.其他中间件.html" title="12.其他中间件" class="sidebar-link">12.其他中间件</a></li><li><a href="/front-end/node/13.restful-api request response.html" title="13.restful-api request response" class="active sidebar-link">13.restful-api request response</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end/node/13.restful-api request response.html#http-status-codes" title="HTTP Status Codes" class="sidebar-link">HTTP Status Codes</a></li><li class="sidebar-sub-header"><a href="/front-end/node/13.restful-api request response.html#sending-a-response" title="Sending a Response" class="sidebar-link">Sending a Response</a></li></ul></li><li><a href="/front-end/node/14.parameter.html" title="14.parameter" class="sidebar-link">14.parameter</a></li><li><a href="/front-end/node/15.express要点.html" title="15.express要点" class="sidebar-link">15.express要点</a></li><li><a href="/front-end/node/16.mongoDB初步.html" title="16.mongoDB初步" class="sidebar-link">16.mongoDB初步</a></li><li><a href="/front-end/node/17.mongoDB1.html" title="17.mongoDB1" class="sidebar-link">17.mongoDB1</a></li><li><a href="/front-end/node/18.mongoui和mongo shell.html" title="18.mongoui和mongo shell" class="sidebar-link">18.mongoui和mongo shell</a></li><li><a href="/front-end/node/19.mongodb实际应用.html" title="19.mongodb实际应用" class="sidebar-link">19.mongodb实际应用</a></li><li><a href="/front-end/node/2.cache imports及几种exports类型.html" title="2.cache imports及几种exports类型" class="sidebar-link">2.cache imports及几种exports类型</a></li><li><a href="/front-end/node/20.mongodb任务.html" title="20.mongodb任务" class="sidebar-link">20.mongodb任务</a></li><li><a href="/front-end/node/21.mongoose.html" title="21.mongoose" class="sidebar-link">21.mongoose</a></li><li><a href="/front-end/node/22.mongoose model 方法.html" title="22.mongoose model 方法" class="sidebar-link">22.mongoose model 方法</a></li><li><a href="/front-end/node/23.model method和doc method.html" title="23.model method和doc method" class="sidebar-link">23.model method和doc method</a></li><li><a href="/front-end/node/24.virtual fields和nested document.html" title="24.virtual fields和nested document" class="sidebar-link">24.virtual fields和nested document</a></li><li><a href="/front-end/node/25.Relationships and Joins with Population.html" title="25.Relationships and Joins with Population" class="sidebar-link">25.Relationships and Joins with Population</a></li><li><a href="/front-end/node/26.mongoose其他.html" title="26.mongoose其他" class="sidebar-link">26.mongoose其他</a></li><li><a href="/front-end/node/3.core modules.html" title="3.core modules" class="sidebar-link">3.core modules</a></li><li><a href="/front-end/node/4.事件.html" title="4.事件" class="sidebar-link">4.事件</a></li><li><a href="/front-end/node/5.http.html" title="5.http" class="sidebar-link">5.http</a></li><li><a href="/front-end/node/6.httpserver.html" title="6.httpserver" class="sidebar-link">6.httpserver</a></li><li><a href="/front-end/node/7.npm.html" title="7.npm" class="sidebar-link">7.npm</a></li><li><a href="/front-end/node/8.get and write.html" title="8.get and write" class="sidebar-link">8.get and write</a></li><li><a href="/front-end/node/9.cvs转json.html" title="9.cvs转json" class="sidebar-link">9.cvs转json</a></li><li><a href="/front-end/node/mongodb 导入.html" title="mongodb 导入" class="sidebar-link">mongodb 导入</a></li><li><a href="/front-end/node/node判断是否为文件夹.html" title="node判断是否为文件夹" class="sidebar-link">node判断是否为文件夹</a></li><li><a href="/front-end/node/npm 升级.html" title="npm 升级" class="sidebar-link">npm 升级</a></li><li><a href="/front-end/node/其他收藏/文件浏览.html" title="文件浏览" class="sidebar-link">文件浏览</a></li><li><a href="/front-end/node/常用命令.html" title="常用命令" class="sidebar-link">常用命令</a></li><li><a href="/front-end/node/获取所有目录.html" title="获取所有目录" class="sidebar-link">获取所有目录</a></li><li><a href="/front-end/node/设置密码.html" title="设置密码" class="sidebar-link">设置密码</a></li></ul></aside> <main class="page"> <div class="theme-antdocs-content content__default"><h1 id="_13-restful-api-request-response"><a href="#_13-restful-api-request-response" class="header-anchor">#</a> 13.restful-api request response</h1> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> express <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;../../node_modules/express&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> app <span class="token operator">=</span> <span class="token function">express</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> bodyParser <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;body-parser&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">use</span><span class="token punctuation">(</span>bodyParser<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">let</span> profile <span class="token operator">=</span> <span class="token punctuation">{</span>
  username<span class="token operator">:</span> <span class="token string">&quot;asdf&quot;</span><span class="token punctuation">,</span>
  email<span class="token operator">:</span> <span class="token string">&quot;[reducted]&quot;</span><span class="token punctuation">,</span>
  url<span class="token operator">:</span> <span class="token string">&quot;http://aaa.com&quot;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/profile&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  res<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>profile<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token string">&quot;/profile&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  profile <span class="token operator">=</span> req<span class="token punctuation">.</span>body<span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;created&quot;</span><span class="token punctuation">,</span> profile<span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">sendStatus</span><span class="token punctuation">(</span><span class="token number">201</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string">&quot;/profile&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  Object<span class="token punctuation">.</span><span class="token function">assign</span><span class="token punctuation">(</span>profile<span class="token punctuation">,</span> req<span class="token punctuation">.</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;updated&quot;</span><span class="token punctuation">,</span> profile<span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">sendStatus</span><span class="token punctuation">(</span><span class="token number">204</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//put 一般是全部修改，单一般用作局部修改，patch才是局部更改</span>

<span class="token comment">//Patch and head is for cross origin resource sharing,跨域用到</span>

app<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token string">&quot;/profile&quot;</span><span class="token punctuation">,</span> <span class="token punctuation">(</span><span class="token parameter">req<span class="token punctuation">,</span> res</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  profile <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">&quot;deleted&quot;</span><span class="token punctuation">,</span> profile<span class="token punctuation">)</span><span class="token punctuation">;</span>
  res<span class="token punctuation">.</span><span class="token function">sendStatus</span><span class="token punctuation">(</span><span class="token number">204</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">listen</span><span class="token punctuation">(</span><span class="token number">3000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>创建一个 sh 文件</p> <div class="language-sh extra-class"><pre class="language-sh"><code><span class="token function">curl</span> <span class="token string">&quot;http://localhost:3000/profile&quot;</span>
<span class="token function">sleep</span> <span class="token number">1</span>
<span class="token function">curl</span> -H <span class="token string">'Content-Type: application/json'</span> -X POST -d <span class="token string">'{&quot;first_name&quot;:&quot;aLieZ&quot;,&quot;last_name&quot;:&quot;zzz&quot;}'</span> <span class="token string">&quot;http://localhost:3000/profile&quot;</span> -i
<span class="token function">sleep</span> <span class="token number">1</span>
<span class="token function">curl</span> -H <span class="token string">'Content-Type: application/json'</span> -X PUT -d <span class="token string">'{&quot;first_name&quot;:&quot;rtz&quot;,&quot;last_name&quot;:&quot;babayefu&quot;}'</span> <span class="token string">&quot;http://localhost:3000/profile&quot;</span> -i
<span class="token function">sleep</span> <span class="token number">1</span>
<span class="token function">curl</span> <span class="token string">&quot;http://localhost:3000/profile&quot;</span>
<span class="token function">sleep</span> <span class="token number">1</span>
<span class="token function">curl</span> -X DELETE <span class="token string">&quot;http://localhost:3000/profile&quot;</span>
</code></pre></div><p>在 bash 中使用 sh ./test.sh 执行 sh 文件中的请求或者使用 Postman</p> <p>If you have two routes in app.js:</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">const</span> <span class="token punctuation">{</span> homePage<span class="token punctuation">,</span> getUsers <span class="token punctuation">}</span> <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">&quot;./routes&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

app<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/&quot;</span><span class="token punctuation">,</span> homePage<span class="token punctuation">)</span><span class="token punctuation">;</span>
app<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/users&quot;</span><span class="token punctuation">,</span> getUsers<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><div class="language-js extra-class"><pre class="language-js"><code>app<span class="token punctuation">.</span><span class="token function">post</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token comment">//POST requests are used for creation of new entities</span>
app<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token comment">//PUT requests are used for complete replacements or partial updates</span>
app<span class="token punctuation">.</span><span class="token function">patch</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token comment">//PATCH requests are used for partial updates</span>
app<span class="token punctuation">.</span><span class="token function">delete</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token comment">//DELETE requests are used for removal of existing entities</span>
app<span class="token punctuation">.</span><span class="token function">head</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token comment">//HEAD requests are idential to GET requests but without the body</span>
app<span class="token punctuation">.</span><span class="token function">options</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">:</span> <span class="token comment">//OPTIONS requests are used to identify allowed methods</span>
app<span class="token punctuation">.</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token comment">//will define the request handler for any HTTP method. app.all('*', fn) is used mostly as a final catch to show 404: Not Found. If you put this first, then nothing will be executed after this route.</span>
</code></pre></div><p>The ==Express request object== has more properties than the core http request from which it extends. These properties simplify development and provide extra functionality.</p> <div class="language-js extra-class"><pre class="language-js"><code>request<span class="token punctuation">.</span>params<span class="token operator">:</span> <span class="token constant">URL</span> parameters
request<span class="token punctuation">.</span>query<span class="token operator">:</span> query string parameters
request<span class="token punctuation">.</span>route<span class="token operator">:</span> current route <span class="token keyword">as</span> a string
request<span class="token punctuation">.</span>cookies<span class="token operator">:</span> cookies<span class="token punctuation">,</span> requires cookieParser
request<span class="token punctuation">.</span>signedCookies<span class="token operator">:</span> signed cookies<span class="token punctuation">,</span> requires cookie<span class="token operator">-</span>parser
request<span class="token punctuation">.</span>body<span class="token operator">:</span> body<span class="token operator">/</span>payload<span class="token punctuation">,</span> requires body<span class="token operator">-</span>parser
request<span class="token punctuation">.</span>headers<span class="token operator">:</span> headers
</code></pre></div><p>There are ==special properties ==and methods which provide access to HTTP request header information. These shortcuts are known as sugarcoating because everything they do can be done with request.headers.</p> <div class="language-js extra-class"><pre class="language-js"><code>request<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>headerKey<span class="token punctuation">)</span> <span class="token comment">//: value for the header key</span>
request<span class="token punctuation">.</span><span class="token function">accepts</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span> <span class="token comment">//: checks if the type is accepted</span>
request<span class="token punctuation">.</span><span class="token function">acceptsLanguage</span><span class="token punctuation">(</span>language<span class="token punctuation">)</span><span class="token operator">:</span> checks language
request<span class="token punctuation">.</span><span class="token function">acceptsCharset</span><span class="token punctuation">(</span>charset<span class="token punctuation">)</span><span class="token operator">:</span> checks charset
request<span class="token punctuation">.</span><span class="token function">is</span><span class="token punctuation">(</span>type<span class="token punctuation">)</span><span class="token operator">:</span> checks the type
request<span class="token punctuation">.</span>ip<span class="token operator">:</span> <span class="token constant">IP</span> address
request<span class="token punctuation">.</span>ips<span class="token operator">:</span> <span class="token constant">IP</span> <span class="token function">addresses</span> <span class="token punctuation">(</span><span class="token keyword">with</span> trust<span class="token operator">-</span>proxy on<span class="token punctuation">)</span>
request<span class="token punctuation">.</span>path<span class="token operator">:</span> <span class="token constant">URL</span> path
request<span class="token punctuation">.</span>host<span class="token operator">:</span> host without port number
request<span class="token punctuation">.</span>fresh<span class="token operator">:</span> checks freshness
request<span class="token punctuation">.</span>stale<span class="token operator">:</span> checks staleness
request<span class="token punctuation">.</span>xhr<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token keyword">for</span> <span class="token constant">AJAX</span><span class="token operator">-</span>y requests
request<span class="token punctuation">.</span>protocol<span class="token operator">:</span> returns <span class="token constant">HTTP</span> protocol
request<span class="token punctuation">.</span>secure<span class="token operator">:</span> checks <span class="token keyword">if</span> protocol is https
request<span class="token punctuation">.</span>subdomains<span class="token operator">:</span> array <span class="token keyword">of</span> subdomains
request<span class="token punctuation">.</span>originalUrl<span class="token operator">:</span> original <span class="token constant">URL</span>
</code></pre></div><p>The response object is used to send the response and to ==<strong>modify</strong>== an HTTP response before sending it out.</p> <p>The Express response object has additional methods to the core http's ==statusCode()==, writeHead(), end() and write().</p> <div class="language-js extra-class"><pre class="language-js"><code>response<span class="token punctuation">.</span><span class="token function">redirect</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token operator">:</span> redirect request
response<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token operator">:</span> send response
response<span class="token punctuation">.</span><span class="token function">json</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token operator">:</span> send <span class="token constant">JSON</span> and force proper headers
response<span class="token punctuation">.</span><span class="token function">sendfile</span><span class="token punctuation">(</span>path<span class="token punctuation">,</span> options<span class="token punctuation">,</span> callback<span class="token punctuation">)</span><span class="token operator">:</span> send a file
response<span class="token punctuation">.</span><span class="token function">render</span><span class="token punctuation">(</span>templateName<span class="token punctuation">,</span> locals<span class="token punctuation">,</span> callback<span class="token punctuation">)</span><span class="token operator">:</span> render a template
response<span class="token punctuation">.</span>locals<span class="token operator">:</span> pass data to template
</code></pre></div><h2 id="http-status-codes"><a href="#http-status-codes" class="header-anchor">#</a> HTTP Status Codes</h2> <p>To specify a status code, use the response object's status function:</p> <div class="language-js extra-class"><pre class="language-js"><code>app<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string">&quot;/user/:id&quot;</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">request<span class="token punctuation">,</span> response</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// Logic to check for user</span>
  <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>exists<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    response<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token number">404</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token keyword">if</span> <span class="token punctuation">(</span>authorized<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    response<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
    response<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token number">401</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  <span class="token comment">// ...</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>HTTP Status Codes</p> <p>2XX: for successfully processed requests</p> <p>3XX: for redirections or cache information</p> <p>4XX: for client-side errors</p> <p>5XX: for server-side errors</p> <h2 id="sending-a-response"><a href="#sending-a-response" class="header-anchor">#</a> Sending a Response</h2> <p>The content-type is determined given the type of argument passed:</p> <div class="language-js extra-class"><pre class="language-js"><code>response<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;Hello World!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Content-type: text/plain</span>
response<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Content-type: application/json</span>
response<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token punctuation">{</span> name<span class="token operator">:</span> <span class="token string">&quot;John Doe&quot;</span> <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Content-type: application/json</span>
</code></pre></div><p>The content-type can also be hardcoded:</p> <div class="language-js extra-class"><pre class="language-js"><code>response<span class="token punctuation">.</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token string">&quot;Content-Type&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;text/plain&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
response<span class="token punctuation">.</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token string">&quot;Just regular text, no html expected!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Some status codes like 204 do not support (according to the W3C specs) a body. Express allows you to send an empty body:</p> <div class="language-js extra-class"><pre class="language-js"><code>response<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token number">204</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div><p>Other status codes can be also used with an empty body:</p> <div class="language-js extra-class"><pre class="language-js"><code>response<span class="token punctuation">.</span><span class="token function">status</span><span class="token punctuation">(</span><span class="token number">404</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">end</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre></div></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">最后更新时间:</span> <span class="time">1/29/2021, 5:19:40 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/front-end/node/12.其他中间件.html" class="prev"><i aria-label="icon: left" class="anticon anticon-left"><svg viewBox="64 64 896 896" focusable="false" data-icon="left" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M724 218.3V141c0-6.7-7.7-10.4-12.9-6.3L260.3 486.8a31.86 31.86 0 0 0 0 50.3l450.8 352.1c5.3 4.1 12.9.4 12.9-6.3v-77.3c0-4.9-2.3-9.6-6.1-12.6l-360-281 360-281.1c3.8-3 6.1-7.7 6.1-12.6z"></path></svg></i>
        12.其他中间件
      </a></span> <span class="next"><a href="/front-end/node/14.parameter.html">
        14.parameter
        <i aria-label="icon: right" class="anticon anticon-right"><svg viewBox="64 64 896 896" focusable="false" data-icon="right" width="1em" height="1em" fill="currentColor" aria-hidden="true"><path d="M765.7 486.8L314.9 134.7A7.97 7.97 0 0 0 302 141v77.3c0 4.9 2.3 9.6 6.1 12.6l360 281.1-360 281.1c-3.9 3-6.1 7.7-6.1 12.6V883c0 6.7 7.7 10.4 12.9 6.3l450.8-352.1a31.96 31.96 0 0 0 0-50.4z"></path></svg></i></a></span></p></div> </main> <!----></div><div class="global-ui"><!----><div id="goTop" class="hide-cat" data-v-bf92849a></div></div></div>
    <script src="/assets/js/app.419c42a0.js" defer></script><script src="/assets/js/3.2e7f4496.js" defer></script><script src="/assets/js/58.0b637d75.js" defer></script>
  </body>
</html>